<?php
/**
 * Created by PhpStorm.
 * User: xpz
 * Date: 2021/10/20
 * Time: 11:10
 */

namespace app\common\model;

use think\Model;
class Poet extends Model
{
// 表名
    protected $name = 'sc_poet';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = '';
    protected $updateTime = '';

    public function getAvatarAttr($value, $data)
    {
//        var_dump(strstr($data['avatar'],'uploads'));
        if(!empty($data['avatar']) && strstr($data['avatar'],'uploads') !== false){
            return getDomain().$data['avatar'];
        } else {
            return "";
        }
    }
    public function getUrlAttr($value, $data){
        return getShirenArticleUrl($data['id']);
    }
    public function getMUrlAttr($value, $data){
        return getMShirenArticleUrl($data['id']);
    }
    public function getSeoDescrAttr($value, $data){
        if(!empty($data['descr'])){
            return str_cut(filter_p($data['descr']));
        } else {
            return $data['name'];
        }

    }
    public function getDynastyUrlAttr($value, $data){
        if(empty($data['dynasty_id'])){
            return getShiciChaodaoUrl("");
        }
        $dynastyData = model('Dynasty')->where(['id'=>$data['dynasty_id']])->cache()->find();
        return getShiciChaodaoUrl($dynastyData['pinyin']);
    }

    public function getLabelAttr($value, $data)
    {
//        var_dump($data['label']);exit;
        if(empty($data['label'])){
            return "";
        }
//        echo $data['label'];exit;
        $labelArr = explode(',',$data['label']);
//        print_r($labelArr);exit;
        $return = [];
        if(!empty($labelArr)){
            $labelModel = model('Label');
            foreach($labelArr as $k=>$v){
                $labelData = $labelModel->where(['name'=>$v])->cache()->find();
                $return[] = $labelData;
            }
            return $return;
        } else {
            return "";
        }
    }

    public function getAddTimeAttr($value, $data){
        if(!empty($data['add_time'])){
            return date('Y-m-d',$data['add_time']);
        } else {
            return date('Y-m-d',time());
        }
    }
}